/**
 * File:		Query.java
 * Project:		Virtual Advisor
 * Module:		Data Management
 * Author:		Rotem Ganel
 * Created:		April 12, 2011
 * Updated:		April 22, 2011
 * Version:		1 (in progress)
 */

package server.dataManagement.Queries;

//TODO: implement search by FIELD (ex: search for CMSC 201 and return x or return all information)

public abstract class Query {
	public static enum ACTION {Create, Read, Update, Delete}
	public static final String[] NO_DATA = {"No data was found matching your query."};
	
	public ACTION action;
	public final String searchFlag;
	private String[] response;
	private boolean fulfilled;
	
	/**
	 * Name:			Query
	 * Description:		constructs a new query
	 * PreCondition:	parameters are valid
	 * PostCondition:	a new Query object is instantiated
	 * Parameters:		ACTION action- defines the type of request being made
	 * 					RETURN_TYPE returnType-	specifies the number of expected responses
	 * Notes:			error checking not yet enforced
	 */
	public Query(ACTION action, String searchFlag) {
		this.action = action;
		this.searchFlag = searchFlag;
		this.fulfilled = false;
	}
	
	/**
	 * Name:			Query
	 * Description:		copy constructor
	 * PreCondition:	the original Query is well-formed
	 * PostCondition:	a deep copy of the Query is instantiated
	 * Parameters:		Query original- the Query to be copied
	 * Notes:			all attributes of the Query are copied, except String[] response and boolean fulfilled,
	 * 					which are reset to the default.
	 */
	public Query(Query original) {
		this(original.action, original.searchFlag);
	}
	
	/**
	 * Name:			(abstract) QUERY_TYPE
	 * Description:		returns the name of the Query subclass type of the instance
	 * PreCondition:  	none
	 * PostCondition: 	the name of the instance type is returned
	 * Parameters:		none
	 * Notes:			this is used in identifying the repository to query.
	 */
	public abstract String QUERY_TYPE();
	
	public abstract String[][] flags();
	
	/**
	 * Name: 			respond
	 * Description: 	fulfills the Query
	 * PreCondition:  	the Query has not yet been filled
	 * PostCondition: 	the Query is filled
	 * 					a boolean is returned verifying the success of the operation
	 * Parameters:		String[] response- a variable number of Strings that fulfills the Query
	 * Notes:			Queries can only be filled once. A Query can be copied in order to receive a new response.
	 */
	public boolean respond(String[] response) {
		if (fulfilled) {
			return false;
		} else {
			this.response = response;
			return (fulfilled = true);
		}
	}
	
	/**
	 * Name: 			fulfilled
	 * Description: 	determines if the Query has been filled yet
	 * PreCondition:  	the Query is well-formed
	 * PostCondition: 	a boolean is returned specifying if the Query has been filled yet
	 * Parameters:		none
	 * Notes:			none
	 */
	public boolean fulfilled() {
		return fulfilled;
	}
	
	/**
	 * Name: 			review
	 * Description: 	returns the contents of the fulfilled response
	 * PreCondition:  	the Query is well-formed
	 * PostCondition: 	if the Query has been filled, the response is returned
	 * 					if the Query has not yet been filled, null is returned
	 * Parameters:		none
	 * Notes:			none
	 */
	public String[] review() {
		//TODO: query not filled yet exception? just return null?
		return response;
	}
}
